<?php include("system/config.inc.php");
$donotinclude = 1;
if(!isset($_SESSION['adminuserid']))
{
	header("location:index.php?msg=logfirst");
	die();
}

// INSERT into database.
if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Submit")
{
	$varsubject = mysql_real_escape_string($_REQUEST['varsubject']);
	$vartext = mysql_real_escape_string($_REQUEST['vartext']);	
	$insert="INSERT INTO `tblnewsletters`
	( `varsubject` , `vartext`)	VALUES ( '$varsubject', '$vartext')";
	$sql=$obj_db->insert($insert);	
	header("location:newsletter.php?msg=add");
	die();
}


// DELETE record from database
if(isset($_REQUEST['a']) && trim($_REQUEST['a'])==3)
{
	if(isset($_REQUEST['intid']) && trim($_REQUEST['intid']!=""))
	{	
		$screen_id =  $_REQUEST['intid'];
		$sql_del = "Delete from tblnewsletters where intid ='$screen_id'";
		$delete = mysql_query($sql_del);
		header("location:newsletter.php");
		die();
	}
}

// UPDATE the record
$varsubject1="";
$vartext1="";
$action=1;

if((isset($_REQUEST['a']) && trim($_REQUEST['a'])==2) && (!(isset($_REQUEST['Submit']) && 
trim($_REQUEST['Submit']) == "Submit")))
{
 	if(isset($_REQUEST['intid']) && trim($_REQUEST['intid'])!="")
 	{
		$screen_id =  $_REQUEST['intid'];
		$sql = "select * from tblnewsletters where intid ='$screen_id'";
		$result = $obj_db->select($sql);
		if(count($result)<=0)
		{
			echo " No Record Found!<br>";
			die();
		}
		if($result)
		{
			foreach($result as $row)
			{
				$varsubject1 = $row['varsubject'];
				$vartext1 = $row['vartext'];
				$action=2;
			}
		}
		if(isset($_REQUEST['Submit']) && trim($_REQUEST['Submit']) == "Update")
		{
				$varsubject = mysql_real_escape_string($_REQUEST['varsubject']);
				$vartext = mysql_real_escape_string($_REQUEST['vartext']);					
				$sql_upd ="UPDATE tblnewsletters SET vartext = '$vartext', varsubject = '$varsubject' WHERE intid ='$screen_id'";			
				$result = $obj_db->sql_query($sql_upd);
				$action=1;
				header("location:newsletter.php");
				die();
		}
	}
}
?>
<?php include("inc/header.php");?>
<body>
<script language="javascript" src="js/newsletter.js"></script>
<script type="text/javascript" src="js/tiny_mce.js"></script>
<script type="text/javascript">
	// O2k7 skin (silver)
	tinyMCE.init({
		// General options
		mode : "exact",
		elements : "vartext",
		theme : "advanced",
		skin : "o2k7",
		skin_variant : "black",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_resizing : false,
		relative_urls : false,
    	convert_urls : false
	});
</script>

<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" class="middlebackground">
<tr>
<td align="center" valign="top" ><?php include(INC."top.inc.php");?></td>
</tr>
<tr>
<td valign="top">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><table width="80%"  border="0" cellspacing="0" cellpadding="0" align="center">
      <tr>
        <td valign="top">
<?php
if(isset($_REQUEST['script']))
{
	if(trim($_REQUEST['script'])=='addnewsletter' || trim($_REQUEST['script'])=='editnewsletter')
	{
	?>
<form name="newsletter" action="" method="post" enctype="multipart/form-data">
  <table align="center" cellpadding="2" cellspacing="2" class="border">
	<tr class="bg1">
	<td align="center"><strong>
	  <?php if($_REQUEST['script']=='addnewsletter'){echo "Add New NewsLetter...";}
	if($_REQUEST['script']=='editnewsletter'){echo "Edit NewsLetter...";}?>
	</strong></td>
	</tr>	
	<tr >
	  <td>	  
	  <table border="0" align="center" cellpadding="2" cellspacing="2">
			<tr>
			  <td align="right" class="sltimg">&nbsp;</td>
			  <td align="right">(* All fields are Required)</td>
			  </tr>
			<tr>
			  <td width="102" align="right" class="sltimg">*Subject : </td>
			  <td width="243"><input name="varsubject" type="text" class="textfield" id="varsubject" value="<?php echo $varsubject1;?>" size="35"></td>
			</tr>
			<tr>
			  <td align="right" valign="top" class="sltimg">*Text : </td>
			  <td><textarea name="vartext" rows="40" id="vartext"><?php echo $vartext1;?></textarea></td>
			  </tr>
			<tr>
			  <td>&nbsp; </td>
			  <td>&nbsp;</td>
			</tr>
			<tr>
			  <td colspan="2" align="center">
				<input name="Submit" type="submit" class="btn" value="<?php echo ($action==2) ? "Update":"Submit"; ?>">	</td>
			</tr>
	  	</table> 	  </td>
	</tr>
	</table></form>

	<?php
	}
}
else
{
?>
<br />
<br />
<br />
<table width="90%" border="0" align="center" cellpadding="1" cellspacing="0">
	  <tr class="bg1">
		<td height="20">
		<table width="100%" border="0" align="center" cellpadding="1" cellspacing="0">
			<tr align="center" class="line_top">
				<td width="100%" align="left"><strong>NewsLetter...</strong></td>
				<td width="100%" align="right">
				 	<a class="aa" href="newsletter.php?script=addnewsletter">[NEW]</a>				</td>
			</tr>
		</table>
		</td>
	  </tr>
		<td class="box">
		<table width="100%"  border="0" align="center" cellpadding="2" cellspacing="2" class="border">
		  <tr>
		    <?php
	   $rows="";
	 if(isset($_REQUEST['page']) && ($_REQUEST['page'])!="")
	 {
	 	$rows=$_REQUEST['page'];
	 
	  }	
	  ?><form name="form1" action="" method="post">
              <td colspan="6" align="left">
			  Row per Page:
                <select name="page" onChange="document.form1.submit();">
                  <option value="0"<?php if($rows=='0'){echo "selected";}else{echo "";}?>>All</option>
                  <option value="5"<?php if($rows=='5'){echo "selected";}else{echo "";}?>>5 Per Page</option>
                  <option value="10"<?php if($rows=='10'){echo "selected";}else{echo "";}?>>10 Per Page</option>
                  <option value="15"<?php if($rows=='15'){echo "selected";}else{echo "";}?>>15 Per Page</option>
                </select>
				</td>
				</form>
		    </tr>
		  <tr class="tblbg3">
		  	<td align="center">No.</td>
			<td align="center">Subject</td>
			<td align="center">Preview</td>
			<td align="center">Edit</td>
			<td align="center">Delete</td>
		  </tr>
	<?php
		/**************************************
		PAGING CODE START
		**************************************/
if(isset($_REQUEST['page']) && ($_REQUEST['page'])!=0)
			{
				$rows=$_REQUEST['page'];
				$rowperpage=$rows;
			}else{$rowperpage=$rows=10000;}
		$tablename="tblnewsletters";
		$per_page_keywords= "";
		$per_page_sorts= "";
		include("system/paging.inc.php");

		/**************************************
		PAGING CODE ENDING
		**************************************/

	
	$sql_select = "select * 
	from  ".$tablename."
	Limit ".($page_no*$row_per_page).",".$row_per_page;
	$sql = $obj_db->select($sql_select);
	if($sql)
	{
	$i=0;
	for($j=0;$j<count($sql);$j++)
	 { 
		$screen_id=$sql[$j]['intid'];
		 $i=$i+1;
		  ?>
			<tr align="center" class="<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>" onMouseOver="this.className='Hrhover';"  
				onMouseOut="this.className='<?php echo ($i%2==0)?"Hrnormal":"Hralter"; ?>';">        
				<td><?php echo $i; ?>.</td>
				<td><?php echo $sql[$j]['varsubject']; ?></td>
				<td><a title="Click here for More Details" href="newsletterdetails.php?&intid=<?php echo $screen_id;?>" class="aa">Preview</a></td>				
				<td><a class="link" Title="Click here to Edit" href="newsletter.php?script=editnewsletter&a=2&intid=<?php echo $screen_id;?>">
				<img src="images/edit.bmp" alt="Edit" border="0"></a></td>
				<td><a Title="Click here to Delete" class="link" href="newsletter.php?a=3&intid=<?php echo $screen_id;?>" 
				onClick="return confirm('Are you sure to delete this record ?');">
				<img src="images/delete.bmp" alt="Delete" border="0"></a></td>	
			</tr>
			
			<tr>
			<?php 
			}
		?>
			  <td colspan="10" ><div align="center">
			  <?php pagindet_atbotttom_page($div_page_no,$page_no,$req_querystr,$total_db_rec,$row_per_page); ?>
              </div></td>
		  </tr>
		  <?php
	}	
   ?>
  </table></td>
  </tr>
</table>	
<?php
}
?></td>
      </tr>
    </table></td>
  </tr>
</table>

</td>
</tr>
<tr><td align="center">&nbsp;</td>
</tr>
</table>
</body>
</html>